﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using PsychveyApp1.classes;


namespace PsychveyApp1
{
    public partial class AnswerSurvey : System.Web.UI.Page
    {
        public bool surveyCompletion;
        //public bool questionCompletion = true;
        public ArrayList questionPanelArray = new ArrayList();
        //public ArrayList answerCheckboxArray = new ArrayList();
        public ArrayList listOfQuestions;
        public Survey currentSurvey;
        public UserAccount currentUser;
        public Answer answer;
        public Question currentQuestion;
        public UserAnswer userAnswer;


        public int currentPageNumber;
        public int noQuestionsPerPage;

        public ArrayList questionNumberLinkArray = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            surveyCompletion = true;
            if (!IsPostBack)
                lblUsername.Text = ((UserAccount)Session["currentUser"]).username;
            //currentSurvey = (Survey)Session["currentSurvey"];
            currentUser = (UserAccount)Session["currentUser"];

            string surveyid = Request.QueryString["surveyid"];
            this.currentSurvey = DBManager.GetSurveyById(surveyid);
            Session["currentSurvey"] = this.currentSurvey;

            lblTitleOfSurvey.Text = currentSurvey.title;
            lblSurveyID.Text = surveyid;

            // ArrayList listOfAnswers = new ArrayList();= new ArrayList();   
            listOfQuestions = DBManager.GetListOfQuestions(currentSurvey.surveyID, currentSurvey.userID);
            //MultipleSelectionAnswerSurveyPanel multipleSelectionAnswerPanel;
            SingleSelectionAnswerSurveyPanel singleSelectionAnswerPanel;
            OpenEndedAnswerSelection openEndedAnswerPanel;
            TrueOrFalseAnswerSurveyPanel trueOrFalseAnswerPanel;
            LikertAnswerSurveyPanel likertAnswerPanel;
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            int start = Convert.ToInt32(Session["CurrentPageNumber"]) * 10 - 10;
            int end = Convert.ToInt32(Session["CurrentPageNumber"]) * 10;
            if (end > listOfQuestions.Count)
                end = listOfQuestions.Count;
            for (int i = start; i < end; i++)
            {
                if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Multiple Selection Multiple Choice (Customized)"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentSurvey.userID, ((Question)listOfQuestions[i]).questionID);
                    ArrayList listOfUserAnswers = DBManager.GetListOfUserAnswers(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    MultipleSelectionAnswerSurveyPanel multipleSelectionAnswerPanel = null;
                    multipleSelectionAnswerPanel = new MultipleSelectionAnswerSurveyPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfUserAnswers);
                    this.questionPanelArray.Add(multipleSelectionAnswerPanel);
                    td.Controls.Add(multipleSelectionAnswerPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Single Selection Multiple Choice (Customized)"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentSurvey.userID, ((Question)listOfQuestions[i]).questionID);
                    ArrayList listOfUserAnswers = DBManager.GetListOfUserAnswers(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    singleSelectionAnswerPanel = null;
                    singleSelectionAnswerPanel = new SingleSelectionAnswerSurveyPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfUserAnswers);
                    this.questionPanelArray.Add(singleSelectionAnswerPanel);
                    td.Controls.Add(singleSelectionAnswerPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Open Ended"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentSurvey.userID, ((Question)listOfQuestions[i]).questionID);
                    ArrayList listOfUserAnswers = DBManager.GetListOfUserAnswers(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    //Answer answer = DBManager.GetOpenEndedAnswerByQuestionID(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestion[i]).questionID);
                    openEndedAnswerPanel = null;
                    openEndedAnswerPanel = new OpenEndedAnswerSelection(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfUserAnswers);
                    this.questionPanelArray.Add(openEndedAnswerPanel);
                    td.Controls.Add(openEndedAnswerPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("True Or False"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentSurvey.userID, ((Question)listOfQuestions[i]).questionID);
                    ArrayList listOfUserAnswers = DBManager.GetListOfUserAnswers(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    trueOrFalseAnswerPanel = null;
                    trueOrFalseAnswerPanel = new TrueOrFalseAnswerSurveyPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfUserAnswers);
                    this.questionPanelArray.Add(trueOrFalseAnswerPanel);
                    td.Controls.Add(trueOrFalseAnswerPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Likert Question"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentSurvey.userID, ((Question)listOfQuestions[i]).questionID);
                    ArrayList listOfUserAnswers = DBManager.GetListOfUserAnswers(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    likertAnswerPanel = null;
                    likertAnswerPanel = new LikertAnswerSurveyPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfUserAnswers);
                    this.questionPanelArray.Add(likertAnswerPanel);
                    td.Controls.Add(likertAnswerPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }


            }
            this.noQuestionsPerPage = 10;
            LinkButton lbt;
            tr = new TableRow();
            td = new TableCell();
            Label lb = new Label();
            lb.Text = "Page Number: ";
            td.Controls.Add(lb);
            //tr.Cells.Add(td);
            for (int i = 0; i <= (listOfQuestions.Count - 1) / this.noQuestionsPerPage; i++)
            {
                // td = new TableCell();
                lb = new Label();
                lb.Text = "|";
                td.Controls.Add(lb);

                if ((i + 1) == Convert.ToInt32(Session["CurrentPageNumber"]))
                {
                    lb = new Label();
                    lb.Text = Convert.ToString(i + 1);
                    lb.ForeColor = System.Drawing.Color.Red;
                    this.questionNumberLinkArray.Add(lb);
                    td.Controls.Add(lb);
                }
                else
                {
                    lbt = new LinkButton();
                    lbt.Text = Convert.ToString(i + 1);
                    lbt.Click += changePageNumber;
                    this.questionNumberLinkArray.Add(lbt);
                    td.Controls.Add(lbt);
                }
            }
            tr.Cells.Add(td);
            mainTable.Rows.Add(tr);


        }



        public void changePageNumber(Object sender, EventArgs e)
        {
            //save all the user answers of the current pages before switching to another page
            //saveUserAnswersOfCurrentPage();

            Session["currentPageNumber"] = Convert.ToInt32(((LinkButton)sender).Text);
            Server.TransferRequest("AnswerSurvey.aspx");
        }


        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {


        }



        protected void btnProceed_Click(object sender, EventArgs e)
        {
            if (ddlUser.SelectedIndex == 1)
            {

                Response.Redirect("UserAccountHome.aspx");
            }
            else if (ddlUser.SelectedIndex == 2)
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void AdminLoginShortcut_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLogin.aspx");
        }

        protected void MySurveyLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MySurvey.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int start = Convert.ToInt32(Session["CurrentPageNumber"]) * 10 - 10;
            for (int h = 0; h < questionPanelArray.Count; h++)
            {
                bool questionCompletion = false;
                if (((Question)listOfQuestions[h+start]).typeOfQuestion.Equals("Multiple Selection Multiple Choice (Customized)"))
                {
                    DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                    for (int a = 0; a < ((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                            questionCompletion = true;
                        }
                    }
                    if (questionCompletion == false)
                    {
                        this.surveyCompletion = false;
                        ((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Text = "Please fill up the question below before submission";
                        ((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (questionCompletion == true)
                    {
                        ((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Visible = false;
                    }
                    
                }


                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Single Selection Multiple Choice (Customized)"))
                {
                    for (int a = 0; a < ((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                            questionCompletion = true;
                            
                        }

                    }
                    if (questionCompletion == false)
                    {
                        this.surveyCompletion = false;
                        ((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Text = "Please fill up the question below before submission";
                        ((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (questionCompletion == true)
                    {
                        ((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Visible = false;
                    }


                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Open Ended"))
                {
                    for (int a = 0; a < ((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (!((OpenEndedAnswerSelection)questionPanelArray[h]).tbxAnswerTextbox.Text.Equals(""))
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((OpenEndedAnswerSelection)questionPanelArray[h]).tbxAnswerTextbox.Text;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                            //DBManager.UpdateAnswer(currentSurvey.surveyID,((UserAccount)Session["currentUser"]).userID, ((Question)listOfQuestions[h+start]).questionID, ((Answer)((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers[a]).answerID,((OpenEndedAnswerSelection)questionPanelArray[h]).tbxAnswerTextbox.Text); 
                            questionCompletion= true;
                            
                        }
                    }
                    if (questionCompletion == false)
                    {
                        this.surveyCompletion = false;
                        ((OpenEndedAnswerSelection)questionPanelArray[h]).questionStatus.Text = "Please fill up the question below before submission";
                        ((OpenEndedAnswerSelection)questionPanelArray[h]).questionStatus.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (questionCompletion == true)
                    {
                        ((OpenEndedAnswerSelection)questionPanelArray[h]).questionStatus.Visible = false;
                    }

                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("True Or False"))
                {
                    for (int a = 0; a < ((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {

                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                            questionCompletion = true;
                            
                        }
                    
                    }
                    if (questionCompletion == false)
                    {
                        this.surveyCompletion = false;
                        ((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Text = "Please fill up the question below before submission";
                        ((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).questionStatus.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (questionCompletion == true)
                    {
                        ((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Visible = false;
                    }
                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Likert Question"))
                {
                    for (int a = 0; a < ((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((LikertAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {

                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                            questionCompletion = true;
                            
                        }
                    }
                    if (questionCompletion == false)
                    {
                        this.surveyCompletion = false;
                        ((LikertAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Text = "Please fill up the question below before submission";
                        ((LikertAnswerSurveyPanel)questionPanelArray[h]).questionStatus.ForeColor = System.Drawing.Color.Red;
                    }
                    else if(questionCompletion == true)
                    {
                        ((LikertAnswerSurveyPanel)questionPanelArray[h]).questionStatus.Visible = false;
                    }
                }
                
            }//end for questionPanelArray

            int completedQuestionsCount = DBManager.countUserCompletedQuestions(currentUser.userID, currentSurvey.surveyID);
            if (completedQuestionsCount == listOfQuestions.Count)
                this.surveyCompletion = true;
            else
                this.surveyCompletion = false;

            if (this.surveyCompletion == true)
            {
                DBManager.InsertUserCompleteSurvey(currentUser, currentSurvey);
                string count = Convert.ToString(DBManager.GetCountUserCompleteSurvey(currentSurvey.surveyID));
                DBManager.UpdateNoOfRespondents(currentSurvey.surveyID, count);
                Response.Redirect("AnswersSubmitted.aspx");
            }
            else {
                lblSurveyStatus.Text = "Not all of the questions have been answered. Please complete to submit.";
            }
        }

        protected void btnSaveAnswers_Click(object sender, EventArgs e)
        {
            saveUserAnswersOfCurrentPage();
        }

        public void saveUserAnswersOfCurrentPage() {
            int start = Convert.ToInt32(Session["CurrentPageNumber"]) * 10 - 10;
            for (int h = 0; h < questionPanelArray.Count; h++)
            {
                if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Multiple Selection Multiple Choice (Customized)"))
                {
                    DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                    for (int a = 0; a < ((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((MultipleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);

                        }
                    }
                }

                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Single Selection Multiple Choice (Customized)"))
                {
                    for (int a = 0; a < ((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((SingleSelectionAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                        }

                    }
                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Open Ended"))
                {
                    for (int a = 0; a < ((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        UserAnswer userAnswer = new UserAnswer();
                        userAnswer.answerID = ((Answer)((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers[a]).answerID;
                        userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                        userAnswer.surveyID = currentSurvey.surveyID;
                        userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                        userAnswer.answerText = ((OpenEndedAnswerSelection)questionPanelArray[h]).tbxAnswerTextbox.Text;
                        DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                        DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                        //DBManager.UpdateAnswer(currentSurvey.surveyID,((UserAccount)Session["currentUser"]).userID, ((Question)listOfQuestions[h+start]).questionID, ((Answer)((OpenEndedAnswerSelection)questionPanelArray[h]).listOfAnswers[a]).answerID,((OpenEndedAnswerSelection)questionPanelArray[h]).tbxAnswerTextbox.Text); 

                    }
                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("True Or False"))
                {
                    for (int a = 0; a < ((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {
                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((TrueOrFalseAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                        }
                    }
                }
                else if (((Question)listOfQuestions[h + start]).typeOfQuestion.Equals("Likert Question"))
                {
                    for (int a = 0; a < ((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers.Count; a++)
                    {
                        if (((CheckBox)((LikertAnswerSurveyPanel)questionPanelArray[h]).answerCheckboxArray[a]).Checked)
                        {

                            UserAnswer userAnswer = new UserAnswer();
                            userAnswer.answerID = ((Answer)((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerID;
                            userAnswer.questionID = ((Question)listOfQuestions[h + start]).questionID;
                            userAnswer.surveyID = currentSurvey.surveyID;
                            userAnswer.userID = ((UserAccount)Session["currentUser"]).userID;
                            userAnswer.answerText = ((Answer)((LikertAnswerSurveyPanel)questionPanelArray[h]).listOfAnswers[a]).answerText;
                            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[h + start]).questionID);
                            DBManager.InsertUserAnswer(userAnswer, currentQuestion, currentSurvey, currentUser);
                        }
                    }
                }

            }
            lblSurveyStatus.Text = "Survey has been updated";
        }
    }
}
  